package com.fxu.config;

import cn.hutool.core.collection.CollUtil;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Collections;
import java.util.List;

/**
 * swagger3文档配置
 */
@Slf4j
@Configuration
@EnableOpenApi
@EnableKnife4j
//@Profile("dev")
public class Swagger3Config {
    public static final String AUTH = "Token";
    public static final String AUTH_DESC = "统一授权登录";
    public static final String BASE_PACKAGE = "com.fxu";
    public static final String API_TITLE = "官网项目";
    public static final String API_DESC = "接口";
    public static final String API_VERSION = "1.0.0";

    @Bean
    public Docket buildDocket() {
        log.info("Swagger3Config");
        return new Docket(DocumentationType.OAS_30).apiInfo(buildApiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))  // 对所有api进行监控
                //.paths(PathSelectors.any()).build();
                // 不显示error开头的请求
                .paths(PathSelectors.regex("/error.*").negate())
                .build()
                // 统一设置tag
                .tags(new Tag(AUTH, AUTH_DESC))
                .securitySchemes(securitySchemes())
                .securityContexts(securityContext());
    }

    /**
     * 基本信息
     * @return ApiInfo
     */
    private ApiInfo buildApiInfo() {
        return new ApiInfoBuilder().title(API_TITLE).description(API_DESC).version(API_VERSION).build();
    }

    /**
     * bearer 授权上下文
     * @return List<SecurityContext>
     */
    private List<SecurityContext> securityContext() {
        SecurityContext context = new SecurityContext(
                defaultAuth(),
                // 配置需要访问授权的请求，效果是对应页面上的请求有没有小锁图标
                PathSelectors.regex("/auth.*").negate(),
                // 配置需要访问授权的请求，效果是对应页面上的请求有没有小锁图标
                each -> true,
                // operationSelector优先级高于上面两个，配置需要访问授权的请求，效果是对应页面上的请求有没有小锁图标
                // 将auth开头的请求和类、方法上有指定注解的请求在swagger页面上放行，不使用jwt bearer token 授权方案
                operationContext -> !operationContext.requestMappingPattern().matches("/auth.*")
        );
        return Collections.singletonList(context);
    }

    /**
     * 使用jwt bearer token 授权方案
     * 对应页面上的请求有小锁图标
     * @return List<SecurityScheme>
     */
    private List<SecurityScheme> securitySchemes() {
        return CollUtil.toList();
    }

    /**
     * bearer 授权范围
     * @return List<SecurityReference>
     */
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return CollUtil.toList(new SecurityReference(AUTH, authorizationScopes));
    }
}